package com.xiaotu.spider.maoyan.pipeline;

import com.xiaotu.common.model.StarAwardModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.spider.APipeline;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @类名 StarAwardsPipeline
 * @日期 2017年7月31日
 * @作者 高海军
 * @功能 艺人获奖数据pipeline
 */
@Component("StarAwardsPipeline")
public class StarAwardsPipeline extends APipeline
{
    private static final Logger LOGGER = LoggerFactory.getLogger(StarAwardsPipeline.class);

    @Override
    protected void doProcess(ResultItems resultItems, Task task)
    {
        List<StarAwardModel> awardList = resultItems.get(StarAwardModel.class.getName());
        if (awardList == null)
            return;

        Map<String, StarAwardModel> awardMap = (new HashSet<>(awardList)).stream().collect
                (Collectors.toMap(award ->
                {
                    String field = DataRedisKeyUtils.getAwardKey(award);
                    LOGGER.warn("[{}] award field:{}", this.getSpiderType(), field);
                    return field;
                }, award -> award));
        cacheHandler.hMSet(Constants.DataRedisKey.MaoYan_Awards_YYYYMMDD.toString(), awardMap);
    }

}